<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreArticlePost;
use App\Services\Service\ArticleService;
use App\Repositories\Model\UserRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\Model\ArticleRepository;

class ArticleController extends Controller
{
    /**
     * ArticleRepository
     *
     * @var Acticles
     */
    private $article;

    /**
     * UserRepository
     *
     * @var User
     */
    private $user;

    /**
     * ArticleService
     *
     * @var ArticleService
     */
    protected $articleService;

    /**
     * 每页显示条数
     *
     * @var int
     */
    protected $rowNum = 50;

    /**
     * ArticleController constructor.
     * @param Acticles $article
     * @param User $user
     * @param ArticleService $articleService
     */
    public function __construct(ArticleRepository $article, UserRepository $user, ArticleService $articleService)
    {
        $this->article = $article;
        $this->user = $user;
        $this->articleService = $articleService;
    }

    /**
     * 文章列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //因为只有一个页面要用到这个查询条件，不使用（通用criteria+门面模式）
        $list = $this->articleService->lists($request,$this->rowNum);
        $userList = $this->user->all();
        //return view('admin/article/index')
        //    ->with(['list' => $ret['list'], 'userList' => $userList, 'where' => $ret['where']]);
        return view('admin/article/index',compact('list', 'userList'));
    }

    /**
     * 创建文章页面
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin/article/create');
    }

    /**
     * 创建文章保存
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreArticlePost $request)
    {
        $ret = $this->articleService->storeArticle($request);
        if (isset($ret['id']) && $ret['id'] > 0){
            return redirect('/article');
        }
        return redirect('/article/create');
    }

    /**
     * 展示某条文章
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = $this->articleService->getById($id);
        $this->authorize('view', $data);
        return view('admin/article/show',compact('data'));
    }

    /**
     * 编辑某条文章
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = $this->articleService->getById($id);
        $this->authorize('update', $data);
        return view('admin/article/edit',compact('data'));
    }

    /**
     * 更新某条文章
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(StoreArticlePost $request, $id)
    {
        $data = $this->articleService->getById($id);
        $this->authorize('update', $data);
        $ret = $this->articleService->updateArticle($request, $id);
        if ($ret)
        {
            return redirect('/article');
        }
        return redirect('/edit/' . $id);
    }

    /**
     * 删除某条文章
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = $this->articleService->getById($id);
        $this->authorize('delete', $data);
        $ret = $this->articleService->destroy($id);
        return redirect('/article');
    }
}
